Page({
  data: {
    time: '12:30',
    currentTab: 'all',
    showWarning: true,
    stats: {
      total: 362,
      available: 156,
      reserved: 43,
      warning: 12
    },
    weddingDresses: [
      {
        id: 1,
        name: '星光蕾丝拖尾婚纱',
        code: 'WD2024-068',
        type: '鱼尾婚纱',
        size: 'XS-XL',
        image: '/assets/images/inventory/wedding_dress_1.jpg',
        totalCount: 12,
        availableCount: 8,
        reservedCount: 3,
        maintenanceCount: 1,
        statusText: '库存充足',
        statusClass: 'in-stock'
      },
      {
        id: 2,
        name: '法式蕾丝荷叶边婚纱',
        code: 'WD2024-045',
        type: '公主风婚纱',
        size: 'XS-L',
        image: '/assets/images/inventory/wedding_dress_2.jpg',
        totalCount: 6,
        availableCount: 2,
        reservedCount: 4,
        maintenanceCount: 0,
        statusText: '库存偏低',
        statusClass: 'low-stock'
      }
    ],
    dresses: [
      {
        id: 3,
        name: '黑色绅士燕尾服',
        code: 'MS2024-023',
        type: '男士套装',
        size: 'M-XXL',
        image: '/assets/images/inventory/suit_1.jpg',
        totalCount: 15,
        availableCount: 10,
        reservedCount: 5,
        maintenanceCount: 0,
        statusText: '可租赁',
        statusClass: 'rental'
      },
      {
        id: 4,
        name: '香槟金晚宴礼服',
        code: 'EG2024-037',
        type: '晚礼服',
        size: 'S-L',
        image: '/assets/images/inventory/dress_1.jpg',
        totalCount: 8,
        availableCount: 0,
        reservedCount: 8,
        maintenanceCount: 0,
        statusText: '已预订',
        statusClass: 'reserved'
      }
    ],
    materials: [
      {
        id: 5,
        name: '法国进口蕾丝面料',
        code: 'FB2024-012',
        type: '面料',
        spec: '150cm×10m',
        image: '/assets/images/inventory/material_1.jpg',
        currentCount: 2,
        safeCount: 10,
        monthlyUsage: 50,
        toPurchase: 48,
        statusText: '库存不足',
        statusClass: 'out-of-stock'
      }
    ]
  },

  onLoad: function() {
    // 初始化时间
    this.updateTime();
    this.getTimerID = setInterval(this.updateTime, 60000);
    
    // 加载库存数据
    this.loadInventoryData();
  },
  
  onUnload: function() {
    clearInterval(this.getTimerID);
  },
  
  updateTime: function() {
    const date = new Date();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    this.setData({
      time: `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
    });
  },
  
  loadInventoryData: function() {
    // 这里可以从API加载库存数据
    // 示例代码:
    /*
    wx.request({
      url: 'https://api.example.com/inventory',
      method: 'GET',
      success: (res) => {
        this.setData({
          stats: res.data.stats,
          weddingDresses: res.data.weddingDresses,
          dresses: res.data.dresses,
          materials: res.data.materials,
          showWarning: res.data.stats.warning > 0
        });
      },
      fail: (err) => {
        console.error('获取库存数据失败', err);
        wx.showToast({
          title: '获取数据失败',
          icon: 'none'
        });
      }
    });
    */
  },
  
  onSearchInput: function(e) {
    const searchText = e.detail.value;
    // 实现搜索逻辑
    console.log('搜索:', searchText);
    
    // 可以添加搜索防抖逻辑
    if (this.searchTimer) clearTimeout(this.searchTimer);
    this.searchTimer = setTimeout(() => {
      this.performSearch(searchText);
    }, 500);
  },
  
  performSearch: function(searchText) {
    // 实现搜索功能
    if (!searchText) {
      // 重置显示所有数据
      this.loadInventoryData();
      return;
    }
    
    // 这里可以从API搜索库存数据
    // 示例代码:
    /*
    wx.request({
      url: 'https://api.example.com/inventory/search',
      method: 'GET',
      data: {
        keyword: searchText
      },
      success: (res) => {
        this.setData({
          weddingDresses: res.data.weddingDresses,
          dresses: res.data.dresses,
          materials: res.data.materials
        });
      }
    });
    */
    
    // 本地搜索示例
    const filterItems = (items) => {
      return items.filter(item => 
        item.name.includes(searchText) || 
        item.code.includes(searchText) || 
        item.type.includes(searchText)
      );
    };
    
    this.setData({
      weddingDresses: filterItems(this.data.weddingDresses),
      dresses: filterItems(this.data.dresses),
      materials: filterItems(this.data.materials)
    });
  },
  
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab
    });
    
    // 根据选中的标签筛选数据
    this.filterByTab(tab);
  },
  
  filterByTab: function(tab) {
    // 重新加载所有数据
    this.loadInventoryData();
    
    // 然后根据标签筛选
    if (tab === 'all') {
      // 显示所有数据，不需要筛选
      return;
    }
    
    // 这里可以从API根据类型获取数据
    // 示例代码:
    /*
    wx.request({
      url: 'https://api.example.com/inventory/filter',
      method: 'GET',
      data: {
        type: tab
      },
      success: (res) => {
        this.setData({
          weddingDresses: tab === 'wedding' ? res.data.items : [],
          dresses: tab === 'dress' ? res.data.items : [],
          materials: tab === 'material' ? res.data.items : []
        });
      }
    });
    */
    
    // 本地过滤示例
    switch(tab) {
      case 'wedding':
        this.setData({
          dresses: [],
          materials: []
        });
        break;
      case 'dress':
        this.setData({
          weddingDresses: [],
          materials: []
        });
        break;
      case 'accessory':
        // 假设配饰暂无数据
        this.setData({
          weddingDresses: [],
          dresses: [],
          materials: []
        });
        break;
      case 'material':
        this.setData({
          weddingDresses: [],
          dresses: []
        });
        break;
      case 'warning':
        // 只显示库存预警的项目
        const filterWarningItems = (items) => {
          return items.filter(item => 
            item.statusClass === 'low-stock' || 
            item.statusClass === 'out-of-stock'
          );
        };
        
        this.setData({
          weddingDresses: filterWarningItems(this.data.weddingDresses),
          dresses: filterWarningItems(this.data.dresses),
          materials: filterWarningItems(this.data.materials)
        });
        break;
    }
  },
  
  viewWarningDetails: function() {
    this.switchTab({
      currentTarget: {
        dataset: {
          tab: 'warning'
        }
      }
    });
  },
  
  quickPurchase: function() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  goToInventoryCheck: function() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  goToPurchaseManagement: function() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  goToInOutManagement: function() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  adjustInventory: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  viewItemDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  applyPurchase: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },
  
  showAddOptions: function() {
    wx.showActionSheet({
      itemList: ['添加婚纱', '添加礼服', '添加配饰', '添加辅料'],
      success: (res) => {
        wx.showToast({
          title: '功能开发中',
          icon: 'none'
        });
      }
    });
  }
}); 